package com.stu.manager.organization.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.stu.manager.config.Result;
import com.stu.manager.organization.entity.SysClassinfo;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.entity.dto.AddClassDto;
import com.stu.manager.organization.entity.dto.TeacherManage;
import com.stu.manager.organization.entity.vo.OneLevel;
import com.stu.manager.organization.entity.vo.TwoLevel;
import com.stu.manager.organization.mapper.SysClassinfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.manager.organization.mapper.SysDepartmentMapper;
import com.stu.manager.organization.mapper.SysProfessionalMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-13
 */
@Service
public class SysClassinfoServiceImpl extends ServiceImpl<SysClassinfoMapper, SysClassinfo> implements IService<SysClassinfo> {

    @Resource
    private SysDepartmentMapper departmentMapper;

    @Resource
    private SysProfessionalMapper professionalMapper;

    //模糊分页查询班级
    public IPage<SysClassinfo> pageClassInfo(Page<SysClassinfo> pageClassInfo, QueryWrapper<SysClassinfo> wrapper) {
        IPage<SysClassinfo> sysClassinfoIPage = baseMapper.selectPage(pageClassInfo, wrapper);
        return sysClassinfoIPage;
    }

    //查询学院专业一二级分类
    public List<OneLevel> getClassLevel() {

        List<OneLevel> oneLevelList = new ArrayList<>();
        List<SysDepartment> sysDepartments = departmentMapper.selectList(null);
        for (SysDepartment sysDepartment : sysDepartments) {
            String departmentName = sysDepartment.getDepartmentName();
            String departmentCode = sysDepartment.getDepartmentCode();
            OneLevel oneLevel = new OneLevel();//给此实体赋值一级学院
            oneLevel.setDepartmentName(departmentName);
            oneLevel.setDepartmentCode(departmentCode);

            List<TwoLevel> twoLevelList = new ArrayList<>();
            QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
            wrapper.eq("department_name", departmentName);
            List<SysProfessional> sysProfessionals = professionalMapper.selectList(wrapper);
            //学院下属专业遍历
            for (SysProfessional sysProfessional : sysProfessionals) {
                String professionalName = sysProfessional.getProfessionalName();
                String professionalCode = sysProfessional.getProfessionalCode();
                TwoLevel twoLevel = new TwoLevel();
                twoLevel.setProfessionalName(professionalName);
                twoLevel.setProfessionalCode(professionalCode);
                twoLevelList.add(twoLevel);//每个二级专业赋给一级学院
            }

            oneLevel.setChildren(twoLevelList);
            oneLevelList.add(oneLevel);//一级分类添加
        }

        return oneLevelList;
    }


    //添加班级
    public Result addClassInfo(AddClassDto addClassDto) {
        String departmentName = addClassDto.getDepartmentName();
        String professionalName = addClassDto.getProfessionalName();


        Integer classGrade = addClassDto.getClassGrade();
        Integer classCode = addClassDto.getClassCode();
        QueryWrapper<SysClassinfo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("class_code", classCode).eq("class_grade", classGrade).eq("professional_name", professionalName);
        Integer count = baseMapper.selectCount(wrapper2);
        if (count > 0) {
            return Result.error().message("当前班级已存在！");
        }


        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_name", departmentName);
        SysDepartment sysDepartment = departmentMapper.selectOne(wrapper);
        String departmentCode = sysDepartment.getDepartmentCode();


        QueryWrapper<SysProfessional> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name", professionalName);
        SysProfessional sysProfessional = professionalMapper.selectOne(wrapper1);
        String professionalCode = sysProfessional.getProfessionalCode();


        SysClassinfo sysClassinfo = new SysClassinfo();
        sysClassinfo.setClassCode(classCode);
        sysClassinfo.setClassGrade(classGrade);
        sysClassinfo.setDepartmentName(departmentName);
        sysClassinfo.setDepartmentCode(departmentCode);
        sysClassinfo.setProfessionalName(professionalName);
        sysClassinfo.setProfessionalCode(professionalCode);

        int insert = baseMapper.insert(sysClassinfo);
        if (insert > 0) {
            return Result.ok().message("添加成功！");
        } else {
            return Result.error().message("添加失败！");
        }
    }

    //查询一个班级信息
    public SysClassinfo getClassInfoById(String id) {
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysClassinfo sysClassinfo = baseMapper.selectOne(wrapper);
        return sysClassinfo;
    }

    //修改班级信息
    public Result updateClassInfo(SysClassinfo classinfo) {
        Integer classGrade = classinfo.getClassGrade();
        Integer classCode = classinfo.getClassCode();
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("class_code",classCode).eq("class_grade",classGrade).eq("professional_name",classinfo.getProfessionalName()).ne("id",classinfo.getId());
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            return Result.error().message("班级代码重复，请修改");
        }

        String departmentName = classinfo.getDepartmentName();
        QueryWrapper<SysDepartment> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("department_name", departmentName);
        SysDepartment sysDepartment = departmentMapper.selectOne(wrapper1);
        String departmentCode = sysDepartment.getDepartmentCode();

        String professionalName = classinfo.getProfessionalName();
        QueryWrapper<SysProfessional> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("professional_name", professionalName);
        SysProfessional sysProfessional = professionalMapper.selectOne(wrapper2);
        String professionalCode = sysProfessional.getProfessionalCode();

        classinfo.setDepartmentCode(departmentCode);
        classinfo.setProfessionalCode(professionalCode);

        int insert = baseMapper.updateById(classinfo);
        if(insert>0){
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }
    }

    //添加或修改管理教师
    public Result addTeacherManage(TeacherManage teacherManage) {
        String id = teacherManage.getId();
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysClassinfo sysClassinfo = new SysClassinfo();
        sysClassinfo.setTecName(teacherManage.getTecName());
        sysClassinfo.setJobNum(teacherManage.getJobNum());
        int update = baseMapper.update(sysClassinfo, wrapper);
        if(update>0){
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }
    }

    public Result getClassCode(String hiredate, String professionalName) {
        QueryWrapper<SysClassinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("class_grade",hiredate).eq("professional_name",professionalName);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            List<SysClassinfo> sysClassinfos = baseMapper.selectList(wrapper);
            return Result.ok().data("sysClassinfos",sysClassinfos);
        }else{
            return Result.error().message("当前年级专业未创建班级,请先创建！");
        }
    }
}
